package java;

public class Question11 {
    public static void main(String[] args) {
        int[] arr = {1,8,6,2,5,4,8,3,7};
        System.out.println(maxArea(arr));
        System.out.println(maxArea1(arr));
    }

    /**
     * 核心：从两边向中间移动，移动较大的指针时，容量只会减小，移动较小的指针时，容量可能增大。所以始终移动较小的指针。
     * @param arr
     * @return
     */
    private static int maxArea1(int[] arr) {
        if (arr == null || arr.length < 2){
            return -1;
        }
        int lift  = 0;
        int right = arr.length - 1;
        int capacity = (right - lift)*Math.min(arr[lift],arr[right]);
        while (lift < right){
            if (arr[lift] <= arr[right]){
                lift ++;
            }else {
                right --;
            }
            int temp = (right - lift)*Math.min(arr[right],arr[lift]);
            if (temp > capacity){
                capacity = temp;
            }
        }

        return capacity;
    }

    private static int maxArea(int[] arr) {
        if (arr == null || arr.length < 2){
            return -1;
        }
        int capacity = 0;
        for (int i = 0; i < arr.length - 1 ; i ++){
            for (int j = i + 1;j < arr.length ; j ++){
                int temp = (j - i)*Math.min(arr[i],arr[j]);
                if (temp > capacity){
                    capacity = temp;
                }
            }
        }
        return capacity;
    }
}
